package com.hurl.waguji.biz.download;

import java.util.List;

import org.testng.log4testng.Logger;

import com.hurl.library.http.HttpException;
import com.hurl.library.http.HttpUtils;
import com.hurl.library.spider.SimpleMultiUrlPattern;
import com.hurl.library.spider.SpiderException;
import com.hurl.library.spider.UrlPattern;
import com.hurl.library.util.StringUtils;
import com.hurl.waguji.base.dataprocess.ProcessParams;
import com.hurl.waguji.persistence.DmGpBO;

class StockCodeHexunProcessor extends BaseDownloadForUpdateProcessor {
    public StockCodeHexunProcessor(ProcessParams params,
			DownloadIdentifier identifier) {
		super(params, identifier);
	}

	private static final Logger log = Logger.getLogger(StockCodeHexunProcessor.class);
    

	@Override
	public void downloadFailure(String url, Throwable e) throws SpiderException {

	}

	@Override
	public UrlPattern getUrlPattern() {
		SimpleMultiUrlPattern pattern = new SimpleMultiUrlPattern();
		pattern.addUrl("http://quote.tool.hexun.com/hqzx/quote.aspx?type=2&market=1&sorttype=0&updown=up&page=1&count=9999");
		pattern.addUrl("http://quote.tool.hexun.com/hqzx/quote.aspx?type=2&market=2&sorttype=0&updown=up&page=1&count=9999");
		pattern.addUrl("http://quote.tool.hexun.com/hqzx/quote.aspx?type=3&market=1&sorttype=0&updown=up&page=1&count=9999");
		pattern.addUrl("http://quote.tool.hexun.com/hqzx/quote.aspx?type=3&market=2&sorttype=0&updown=up&page=1&count=9999");
		return pattern;
	}


	@Override
	public void validate() throws Throwable {
		if(isAlreadyDownloaded())
			throw new DownloadException("已经下载过！");
	}

	@Override
	public void convertHTML2BO(String url, String html, BOSaver collector) {
		html = StringUtils.substringBetween(html, "dataArr = [","];");
		if(StringUtils.isBlank(html)){
			return ;
		}
		String prefix = null;
		String lx = null;
		if(url.contains("type=2&market=1")){
			prefix = "SH";
			lx = "SHA";
		}else if(url.contains("type=2&market=2")){
			prefix = "SZ";
			lx = "SZA";
		}else if(url.contains("type=3&market=1")){
			prefix = "SH";
			lx = "SHB";
		}else if(url.contains("type=3&market=2")){
			prefix = "SZ";
			lx = "SZB";
		}
			
		List<String> list = StringUtils.substringsBetween(html, "[", "]");
		DmGpBO bo = null;
		if(url.contains("type=2&market=1")){
			prefix = "SH";
			lx = "SHA";
		}else if(url.contains("type=2&market=2")){
			prefix = "SZ";
			lx = "SZA";
		}else if(url.contains("type=3&market=1")){
			prefix = "SH";
			lx = "SHB";
		}else if(url.contains("type=3&market=2")){
			prefix = "SZ";
			lx = "SZB";
		}
			
		for(String text : list){
			String[] itemarr = text.split(",");
			bo = new DmGpBO();
			bo.setGpDm(prefix+StringUtils.substringBetween(itemarr[0],"'","'"));
			bo.setGpDmJ(StringUtils.substringBetween(itemarr[0],"'","'"));
			bo.setGpMc(StringUtils.substringBetween(itemarr[1],"'","'"));
			bo.setGplxDm(lx);
			bo.setFlag('Y');
			bo.setGxrq(currentTime);
			
			try {
				String pyjx = HttpUtils.request("http://data.stock.hexun.com/include/AjaxSearch2011.ashx?key="+bo.getGpDmJ());
				pyjx = StringUtils.substringBetween(pyjx, "short:'", "'");
				if(pyjx!=null){
					pyjx = pyjx.toUpperCase();
				}
				bo.setPyjx(pyjx);
			} catch (HttpException e) {
				e.printStackTrace();
				log.error(e);
			}
			
			collector.addBO(bo);
		}
	}

	@Override
	public void shutdown(boolean success, Throwable t) {
	}

}
